package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.Client;
import com.acme.bankapp.domain.bank.Gender;

import java.util.Random;

public class BankApplication {
    public static void main(String[] args) {
        BankService bankService = new BankService();
        Bank bank = null;
        try {
            bank = createArbitraryBank();
        } catch (ClientExistsException e) {
            System.out.println(String.format("Client %s already exists", e.getClient()));
        }
        System.out.println("BEFORE MODIFY");
        bankService.printBalance(bank);
        bankService.printMaximumAmountToWithdraw(bank);
        System.out.println("MODIFY");
        try {
            bankService.modifyBank(bank);
        } catch (NotEnoughFundsException e) {
            System.out.println(String.format("Cannot withdraw money from client. Client has only %s, and maximum amount to withdraw is %s",
                    e.getAmount(), e.getMaximumAmountToWithdraw()));
        }
        System.out.println("AFTER MODIFY");
        bankService.printBalance(bank);
        bankService.printMaximumAmountToWithdraw(bank);
    }

    private static Bank createArbitraryBank() throws ClientExistsException {
        Bank bank = new Bank();
        Random random = new Random();
        int numberOfClients = random.nextInt(10);
        for (int i = 0; i < numberOfClients; i++) {
            Gender gender = i % 2 == 0 ? Gender.MALE : Gender.FEMALE;
            Client client = new Client("Client" + i, gender);
            client.addAccount(new SavingAccount(random.nextInt(100)));
            client.addAccount(new CheckingAccount(random.nextInt(100), random.nextInt(100)));
            bank.addClient(client);
        }
        return bank;
    }
}